package us.gaaoc.framework.webapp.security;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.transaction.annotation.Transactional;

import us.gaaoc.framework.dao.ModelFacade;
import us.gaaoc.framework.manager.RoleManager;
import us.gaaoc.framework.model.Court;
import us.gaaoc.framework.model.Organizations;
import us.gaaoc.framework.model.Person;
import us.gaaoc.framework.model.PersonOrganizations;
import us.gaaoc.framework.model.Roles;
import us.gaaoc.framework.webapp.backingbeans.profile.Menu;

/**
 * Handler called when a user has logged in successfully. Handles
 * redirection to the proper ui module and setting up of the session for
 * the JSF views.
 * 
 * @author artripa
 *
 */
public class GajeSuccessfulAuthenticationHandler implements AuthenticationSuccessHandler {

	@Autowired
	ModelFacade modelFacade;

	@Override
	@Transactional
	public void onAuthenticationSuccess(HttpServletRequest request,
			HttpServletResponse response, Authentication authentication)
			throws IOException, ServletException {
		HttpSession sess = request.getSession();
		Person person = modelFacade.getPersonByUserName(authentication.getName());

		String succesString = "";
		Menu menu = null;
		Court court = null;

		if (person.getOrganizations().size() >= 2) {
			//sess.setAttribute("person", person);
			sess.setAttribute("personUuid",person.getUuid());			
			response.sendRedirect(
					request.getContextPath() + 
					"/chooseCourt.faces");
			return;
			
		} else {
			String courtUuid = person.getOrganizations().get(0).getCourtUuid();
			court = modelFacade.findCourtByUuid(courtUuid);
			sess.setAttribute("courtName", court.getName());

			List<Roles> personRoles = person.getRoles();

			RoleManager roleManager = new RoleManager(false);
			menu = roleManager.manageTabForUser( court.getName(),personRoles);

			for (Roles r : personRoles) {
				sess.setAttribute("role", r);
				if (r.getRole().contains("admin")) {
					succesString = request.getContextPath() + "/secure/admin/main.faces";	
				} else if (r.getRole().contains("clerk")) {
					succesString = request.getContextPath() + "/secure/clerk/main.faces";
				} else if (r.getRole().equals("agent")) {
					succesString = request.getContextPath() + "/secure/agent/allFilingsTablePage.faces";
				} else if (r.getRole().equals("attorney")) {
					succesString = request.getContextPath() + "/secure/attorney/allFilingsTablePage.faces";
				} 
			}
		}

		List<Organizations> personOrgs = new ArrayList<Organizations>();
		for (PersonOrganizations o : person.getOrganizations()) {
			Hibernate.initialize(o.getOrganization());
			personOrgs.add(o.getOrganization());
		}

		sess.setAttribute("menu", menu);
		sess.setAttribute("person", person);
		sess.setAttribute("court", court);
		sess.setAttribute("courtUuid", court.getUuid());
		sess.setAttribute("personOrgs", personOrgs);

		response.sendRedirect(succesString);
	
	}

}
